home *** CD-ROM | disk | FTP | other *** search
/ Total Network Tools 2002 / NextStepPublishing-TotalNetworkTools2002-Win95.iso / Archive / Misc Servers / Zope.exe / FIXBBBTS.PY < prev    next >
Encoding:
Python Source  |  1999-04-13  |  2.4 KB  |  92 lines

  1. import sys
  2.  
  3. __doc__="""Fix BoboPOS time stamps
  4.  
  5. If a system has a problem with it's clock setting, it may cause
  6. database records to be written with time stamps in the future.  This
  7. causes problems when the clock is fixed or when the data are moved to
  8. a system that doesn't have a broken clock.
  9.  
  10. The database has a requirement that records should be chronologically
  11. ordered and that times not be in the future.
  12.  
  13. This copies a database, restamping the times as it goes.
  14.  
  15. %s [options] file1 file2
  16.  
  17. Copy file1 to file2 restamping the records.
  18.  
  19.   options:
  20.  
  21.     -o offset
  22.  
  23.        Records that are later offset seconds in the past
  24.        are moved back to offset seconds in the past plus
  25.        some small offset chosen so that times are not the same and
  26.        are chronological.
  27.  
  28. """ % sys.argv[0]
  29.  
  30. InvalidFormat='Format Error'
  31. Corrupted='Data Corruption'
  32.  
  33. def main():
  34.     import getopt, string, struct, time
  35.     file__version__=3.0
  36.     packed_version='SDBMV'+struct.pack(">f",file__version__)
  37.  
  38.     try:
  39.         opts, args = getopt.getopt(sys.argv[1:], 'o:')
  40.         file1, file2 = args
  41.         offset=86400
  42.         for o, v in opts:
  43.             if o=='-o':
  44.                 offset=string.atoi(v)
  45.     except:
  46.         print __doc__
  47.         print "%s: %s" % sys.exc_info()[:2]
  48.  
  49.     start=time.time()-offset
  50.     next=start+0.001
  51.     input=open(file1,'rb')
  52.     read=input.read
  53.     output=open(file2,'wb')
  54.     write=output.write
  55.     pack=struct.pack
  56.     unpack=struct.unpack
  57.     
  58.     h=read(len(packed_version))
  59.     if h != packed_version:
  60.         raise InvalidFormat, 'This is not a BoboPOS file'
  61.     write(h)
  62.  
  63.     pos=len(h)
  64.  
  65.     while 1:
  66.         h=read(24)
  67.         if not h: break
  68.         if len(h) < 24: raise Corrupted, pos
  69.         oid, prev, t, tlen, plen = unpack(">iidii", h)
  70.         if start is None or t > start:
  71.             t=next
  72.             next=next+0.001
  73.             start=None
  74.         if plen > tlen or tlen < 28: raise Corrupted, pos
  75.         
  76.         write(pack(">iidii", oid, prev, t, tlen, plen))
  77.         l=tlen-28
  78.         s=8196
  79.         while l > 0:
  80.             if s > l: s=l
  81.             d=read(s)
  82.             if not d: raise Corrupted, pos
  83.             write(d)
  84.             l=l-len(d)
  85.         d=read(4)
  86.         if d != h[16:20]: raise Corrupted, pos
  87.         write(d)
  88.         pos=pos+tlen
  89.  
  90. if __name__=='__main__': main()
  91.             
  92.